<script>
    $('a#updatePaper').click(function(e) {
            var id = $(this).attr('paperID');
            var page = baseurl + 'admin/loadUpdatePaper_view?id=' + id;
            $.nmManual(page);
        });
</script>
<script>
    $(document).ready(function(){
        
    
        $("#paper").submit(function(e){
            e.preventDefault();
            var url = baseurl + "admin/uploadPaper";
            var description = document.getElementById("description").value;
            var date = document.getElementById("date").value;
            var name = document.getElementById("name").value;
            var file = document.getElementById("file").value;
            $.ajaxFileUpload({
                url: url,
                secureuri:false,
                fileElementId: 'file',
                dataType: 'json',
                data: {
                    'date': date,
                    'name': name,
                    'description': description,
                    'file': file
                },
                success: function(data, status){
                    alert(data.msg);
                    var page = baseurl + 'admin/showPaper';
                    $.get(page)
                    .success(function(data) {
                        $('div#content').html(data);
                    });
                }
            
            
            })
        });
    })
    
    function deletePaper(id){
        var r = confirm("Are you confirm to delete this paper?");
        if(r == true){
            var success = baseurl+"admin/deletePaper?id=" + id;
            $.get(success)
            .success(function(data) {
                if(data == "success"){
                    alert("Successfully deleted!");
                    var page = baseurl + 'admin/showPaper';
                    $.get(page)
                    .success(function(data) {
                        $('div#content').html(data);
                    });
                } else {
                    alert("No change made!");
                }
            });
        } 
        return false;
    }

</script>

<h4>Papers</h4>
<hr>
<div class="row">
    <form method='post' id="paper">
        <fieldset>
            <legend>Add Paper</legend>
            <ul class="inline">
                <li class="field">
                    <label class="inline" style="width: 20%">Publication Date&nbsp;</label>
                    <input class="narrow text input" type="date" placeholder="Text input" name="date" id="date"/>
                </li>
                <li class="field">
                    <label class="inline" style="width: 20%">Name&nbsp;</label>
                    <input class="narrow text input" type="text" placeholder="Name" name="name" required id="name"/>
                </li>
                <li class="field">
                    <label class="inline" style="vertical-align: top; width: 20%">Description</label>
                    <textarea class="wide input textarea" type="textarea" placeholder="Description" required name="description" id="description"></textarea>
                </li>
                <li class="field">
                    <label class="inline" style="width: 20%">File&nbsp;</label>
                    <input class="narrow text input" type="file" placeholder="File" name="file" required id="file"/>
                </li>
                <li>
                    <label class="inline" style="width: 20%">&nbsp;&nbsp;&nbsp;</label>
                    <div class="pretty medium default btn"><input type="submit" value="Submit"></div>
                </li>
            </ul>
        </fieldset>
    </form>
</div>
<?php
// put your code here
if (isset($data) && count($data)) {
    ?>
    <table class="dataTable" id="paper">
        <thead>
        <th>Date</th>
        <th>Name</th>
        <th>Description</th>
        <th>File</th>
        <th></th>
        <th></th>
    </thead>
    <tbody>
        <?php
        foreach ($data as $paper) {
            $id = $paper['id'];
            $file = $paper['filepath'] . $paper['filename'];
            echo "<tr>";
            echo "<td>" . $paper['date'] . "</td>";
            echo "<td>" . $paper['name'] . "</td>";
            echo "<td>" . "<i class='icon-docs'></i>" . "<a href='$file' target='_blank'>Click to download</a>" . "</td>";
            echo "<td>" . $paper['description'] . "</td>";
            echo "<td><a id='updatePaper' href='#' paperID='$id'>Update</a></td>";
            echo "<td><a href='#' onclick='deletePaper($id)'>Delete</a></td>";
            echo "</tr>";
        }
        ?>
    </tbody>
    </table>
    <?php
} else {
    echo "No file uploaded!";
}
?>

